package com.example.test.activity;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import com.example.test.R;
import com.example.test.R.id;
import com.example.test.R.layout;
import com.example.test.R.menu;
import com.example.test.dao.DataBaseHelper;
import com.example.test.model.CommonFunc;
import com.example.test.model.Constants;
import com.example.test.model.social_api.SinaAPI;
import com.netease.microblog.oauth2.Oauth2;
import com.renn.rennsdk.AccessToken;
import com.renn.rennsdk.RennClient;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuth;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.legacy.StatusesAPI;
import com.sina.weibo.sdk.openapi.legacy.WeiboAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXTextObject;
import com.tencent.weibo.sdk.android.model.GlobalVar;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {


	Button btRegister;
	Button btLogin;
	EditText etName;
	EditText etPasswd;
	MainActivity This;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    SharedPreferences preferences = MainActivity.this.getSharedPreferences("default", MODE_PRIVATE);
		This=this;
		etName=(EditText) findViewById(R.id.etAccount);
		etPasswd=(EditText)findViewById(R.id.etPasswd);
		etName.setText(preferences.getString("account", ""));
		etPasswd.setText(preferences.getString("passwd", ""));
		btRegister=(Button)findViewById(R.id.register);
          
		btRegister.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, RegisterActivity.class);
				startActivity(intent);
				
			}
		});
		
		btLogin=(Button)findViewById(R.id.login);
		
		btLogin.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
			SQLiteDatabase database=DataBaseHelper.getRedDatabase(This);
				Cursor cursor=  database.query("account", new String[]{"sina_token","sina_uid","qq_token","renn_token","net_token","sohu_token"}, "name=? and passwd =?", new String[]{etName.getText().toString(),etPasswd.getText().toString()}, null, null, null);
				if(cursor.getCount()>0)
				{
					SharedPreferences preferences = MainActivity.this.getSharedPreferences("default", MODE_PRIVATE);
					Editor editor=preferences.edit();
					editor.putString("account", etName.getText().toString());
					editor.putString("passwd", etPasswd.getText().toString());
					editor.commit();
					cursor.moveToFirst();
					GlobalVar.userName=etName.getText().toString();
					GlobalVar.sinaAccessToken=new Oauth2AccessToken(cursor.getString(0), null);
					GlobalVar.sinaUid=cursor.getString(1);
					GlobalVar.QQToken=cursor.getString(2);
					RennClient rennClient = RennClient.getInstance(getApplicationContext());//获取实例
					rennClient.init("265274", "02c19c5423bd47cb8803dfedafa2b7ae", "7d35459724a34c338f631d697f9ab424");
					GlobalVar.rennClient=rennClient;
					AccessToken accessToken =new AccessToken();
					accessToken.accessToken=cursor.getString(3);
					GlobalVar.rennClient.setAccessToken(accessToken);
					GlobalVar.rennToken=cursor.getString(3);
					Oauth2.getInstance().setAccessToken(cursor.getString(4));
					com.sohu.microblog.oauth2.Oauth2.getInstance().setAccessToken(cursor.getString(5));
				Intent intent=new Intent();
				intent.setClass(MainActivity.this, MainTabActivity.class);
				startActivity(intent);
			    This.finish();
				}
				else
				{
					Toast.makeText(This, "账号或密码错误！", 1000).show();
				}
				
			}
		});
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	
}
